/*      */ package dm.sql;
/*      */ 
/*      */ import dm.jdbc.dataConvertion.Convertion;
/*      */ import dm.jdbc.dbaccess.Const;
/*      */ import dm.jdbc.dbaccess.DBError;
/*      */ import dm.jdbc.driver.DmdbType;
/*      */ import dm.jdbc.util.StringUtil;
/*      */ import java.sql.Date;
/*      */ import java.sql.SQLException;
/*      */ import java.sql.Time;
/*      */ import java.sql.Timestamp;
/*      */ import java.util.ResourceBundle;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ public class DmdbDateTime
/*      */   implements Cloneable
/*      */ {
/*   42 */   final int DMDT_YEAR_POS = 0;
/*      */   
/*   44 */   final int DMDT_YEAR_LEN = 15;
/*      */   
/*   46 */   final int DMDT_MONTH_POS = 15;
/*      */   
/*   48 */   final int DMDT_MONTH_LEN = 4;
/*      */   
/*   50 */   final int DMDT_DAY_POS = 19;
/*      */   
/*   52 */   final int DMDT_DAY_LEN = 5;
/*      */   
/*   54 */   final int DMDT_DATE_SIZE = 24;
/*      */   
/*   56 */   final int DMDT_HOUR_POS = 0;
/*      */   
/*   58 */   final int DMDT_HOUR_LEN = 5;
/*      */   
/*   60 */   final int DMDT_MIN_POS = 5;
/*      */   
/*   62 */   final int DMDT_MIN_LEN = 6;
/*      */   
/*   64 */   final int DMDT_SEC_POS = 11;
/*      */   
/*   66 */   final int DMDT_SEC_LEN = 6;
/*      */   
/*   68 */   final int DMDT_MSEC_POS = 17;
/*      */   
/*   70 */   final int DMDT_MSEC_LEN = 20;
/*      */   
/*   72 */   byte[] dateTime = new byte[3];
/*      */   
/*      */   public static final int DMDT_DATE = 0;
/*      */   
/*      */   public static final int DMDT_TIME = 1;
/*      */   
/*      */   public static final int DMDT_TIMESTAMP = 2;
/*      */   private int prec;
/*      */   
/*      */   public DmdbDateTime(byte[] dt, int dtype, int scale, short dbTz, short lTz)
/*      */     throws SQLException
/*      */   {
/*   84 */     this.prec = scale;
/*   85 */     this.dateTime = dt;
/*      */     
/*   87 */     if ((dtype == 16) && (DmdbType.isLocalTimeZone(dtype, scale)))
/*      */     {
/*   89 */       byte[] temp = dmdtDecodeFast();
/*   90 */       this.dateTime = dmdtAddByFmt(temp, 5, lTz - dbTz);
/*   91 */       this.prec = DmdbType.localTimeZoneRealPrec(this.prec);
/*      */     }
/*      */   }
/*      */   
/*      */   public int hashCode()
/*      */   {
/*   97 */     String strVal = getDateInString() + " " + getTimeInString();
/*      */     
/*   99 */     return strVal.hashCode();
/*      */   }
/*      */   
/*      */   public boolean equals(Object o)
/*      */   {
/*  104 */     if (o == null) {
/*  105 */       return false;
/*      */     }
/*  107 */     if (!(o instanceof DmdbDateTime)) {
/*  108 */       return false;
/*      */     }
/*  110 */     DmdbDateTime tm = (DmdbDateTime)o;
/*  111 */     byte[] bs = tm.dateTime;
/*  112 */     if (this.dateTime.length != bs.length) {
/*  113 */       return false;
/*      */     }
/*  115 */     int len = this.dateTime.length;
/*  116 */     for (int i = 0; i < len; i++)
/*      */     {
/*  118 */       if (this.dateTime[i] != bs[i]) {
/*  119 */         return false;
/*      */       }
/*      */     }
/*  122 */     return true;
/*      */   }
/*      */   
/*      */   public DmdbDateTime(String str, int prec, int flag) throws SQLException
/*      */   {
/*  127 */     this.prec = prec;
/*      */     
/*  129 */     this.dateTime = new byte[12];
/*      */     
/*  131 */     if (flag == 0)
/*      */     {
/*  133 */       String[] subStr = StringUtil.split(str, "-");
/*  134 */       short year = Short.parseShort(subStr[0]);
/*  135 */       if (year > 9999)
/*  136 */         DBError.throwSQLException(6007);
/*  137 */       byte month = Byte.parseByte(subStr[1]);
/*  138 */       byte day = Byte.parseByte(subStr[2]);
/*  139 */       byte[] y = Convertion.shortToByteArray(year);
/*  140 */       this.dateTime[0] = y[0];
/*  141 */       this.dateTime[1] = y[1];
/*  142 */       this.dateTime[2] = month;
/*  143 */       this.dateTime[3] = day;
/*      */     }
/*  145 */     else if (flag == 2)
/*      */     {
/*  147 */       String[] subStr = StringUtil.split(str, "-: .");
/*  148 */       short year = Short.parseShort(subStr[0]);
/*  149 */       if (year > 9999)
/*  150 */         DBError.throwSQLException(6007);
/*  151 */       byte month = Byte.parseByte(subStr[1]);
/*  152 */       byte day = Byte.parseByte(subStr[2]);
/*  153 */       byte[] y = Convertion.shortToByteArray(year);
/*  154 */       this.dateTime[0] = y[0];
/*  155 */       this.dateTime[1] = y[1];
/*  156 */       this.dateTime[2] = month;
/*  157 */       this.dateTime[3] = day;
/*  158 */       byte hour = Byte.parseByte(subStr[3]);
/*  159 */       byte minute = Byte.parseByte(subStr[4]);
/*  160 */       byte sec = Byte.parseByte(subStr[5]);
/*  161 */       this.dateTime[4] = hour;
/*  162 */       this.dateTime[5] = minute;
/*  163 */       this.dateTime[6] = sec;
/*  164 */       if (subStr.length > 6)
/*      */       {
/*  166 */         StringBuffer strNano = new StringBuffer(10);
/*  167 */         strNano.append("0");
/*  168 */         strNano.append(".");
/*  169 */         strNano.append(subStr[6]);
/*  170 */         double d_v = Double.valueOf(strNano.toString()).doubleValue();
/*  171 */         int nano = (int)(d_v * 1000000.0D);
/*  172 */         byte[] msec = Convertion.IntToByteArray(nano);
/*  173 */         System.arraycopy(msec, 0, this.dateTime, 7, 3);
/*      */       }
/*      */     }
/*  176 */     else if (flag == 1)
/*      */     {
/*  178 */       short year = 1900;
/*  179 */       if (year > 9999)
/*  180 */         DBError.throwSQLException(6007);
/*  181 */       byte[] y = Convertion.shortToByteArray(year);
/*  182 */       this.dateTime[0] = y[0];
/*  183 */       this.dateTime[1] = y[1];
/*  184 */       this.dateTime[2] = 1;
/*  185 */       this.dateTime[3] = 1;
/*  186 */       String[] subStr = StringUtil.split(str, ": .");
/*  187 */       byte hour = Byte.parseByte(subStr[0]);
/*  188 */       byte minute = Byte.parseByte(subStr[1]);
/*  189 */       byte sec = Byte.parseByte(subStr[2]);
/*  190 */       this.dateTime[4] = hour;
/*  191 */       this.dateTime[5] = minute;
/*  192 */       this.dateTime[6] = sec;
/*  193 */       if (subStr.length > 3)
/*      */       {
/*  195 */         StringBuffer strNano = new StringBuffer(10);
/*  196 */         strNano.append("0");
/*  197 */         strNano.append(".");
/*  198 */         strNano.append(subStr[3]);
/*  199 */         double d_v = Double.valueOf(strNano.toString()).doubleValue();
/*  200 */         int nano = (int)(d_v * 1000000.0D);
/*  201 */         byte[] msec = Convertion.IntToByteArray(nano);
/*  202 */         System.arraycopy(msec, 0, this.dateTime, 7, 3);
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */   public DmdbDateTime(int[] dt, int prec, int flag)
/*      */     throws SQLException
/*      */   {
/*  210 */     this.prec = prec;
/*      */     
/*  212 */     this.dateTime = new byte[12];
/*      */     
/*  214 */     if (flag == 0)
/*      */     {
/*  216 */       short year = (short)dt[0];
/*  217 */       if (year > 9999)
/*  218 */         DBError.throwSQLException(6007);
/*  219 */       byte month = Byte.parseByte(String.valueOf(dt[1]));
/*  220 */       byte day = Byte.parseByte(String.valueOf(dt[2]));
/*  221 */       byte[] y = Convertion.shortToByteArray(year);
/*  222 */       this.dateTime[0] = y[0];
/*  223 */       this.dateTime[1] = y[1];
/*  224 */       this.dateTime[2] = month;
/*  225 */       this.dateTime[3] = day;
/*      */     }
/*  227 */     else if (flag == 2)
/*      */     {
/*  229 */       short year = (short)dt[0];
/*  230 */       if (year > 9999)
/*  231 */         DBError.throwSQLException(6007);
/*  232 */       byte month = Byte.parseByte(String.valueOf(dt[1]));
/*  233 */       byte day = Byte.parseByte(String.valueOf(dt[2]));
/*  234 */       byte[] y = Convertion.shortToByteArray(year);
/*  235 */       this.dateTime[0] = y[0];
/*  236 */       this.dateTime[1] = y[1];
/*  237 */       this.dateTime[2] = month;
/*  238 */       this.dateTime[3] = day;
/*  239 */       byte hour = Byte.parseByte(String.valueOf(dt[3]));
/*  240 */       byte minute = Byte.parseByte(String.valueOf(dt[4]));
/*  241 */       byte sec = Byte.parseByte(String.valueOf(dt[5]));
/*  242 */       this.dateTime[4] = hour;
/*  243 */       this.dateTime[5] = minute;
/*  244 */       this.dateTime[6] = sec;
/*  245 */       int nano = dt[6];
/*  246 */       byte[] msec = Convertion.IntToByteArray(nano);
/*  247 */       System.arraycopy(msec, 0, this.dateTime, 7, 3);
/*      */     }
/*      */   }
/*      */   
/*      */   public byte[] getByteArrayValue()
/*      */   {
/*  253 */     return this.dateTime;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */   short getYear()
/*      */   {
/*      */     int y;
/*      */     
/*      */ 
/*      */     int y;
/*      */     
/*      */ 
/*  267 */     if (this.dateTime.length == 12)
/*      */     {
/*  269 */       byte[] byte_tmp = new byte[2];
/*  270 */       byte_tmp[0] = this.dateTime[0];
/*  271 */       byte_tmp[1] = this.dateTime[1];
/*  272 */       y = Convertion.twoByteToShort(byte_tmp);
/*      */     }
/*      */     else
/*      */     {
/*  276 */       y = getBit_ex(this.dateTime);
/*      */     }
/*  278 */     return (short)y;
/*      */   }
/*      */   
/*      */   byte getMonth()
/*      */   {
/*      */     int b;
/*      */     int b;
/*  285 */     if (this.dateTime.length == 12) {
/*  286 */       b = this.dateTime[2];
/*      */     } else {
/*  288 */       b = getBit(this.dateTime, 15, 4);
/*      */     }
/*  290 */     return (byte)b;
/*      */   }
/*      */   
/*      */   byte getDay() {
/*      */     int b1;
/*      */     int b1;
/*  296 */     if (this.dateTime.length == 12) {
/*  297 */       b1 = this.dateTime[3];
/*      */     } else {
/*  299 */       b1 = getBit(this.dateTime, 19, 5);
/*      */     }
/*  301 */     return (byte)b1;
/*      */   }
/*      */   
/*      */   byte getHour(int offset)
/*      */   {
/*      */     int b;
/*      */     int b;
/*  308 */     if (this.dateTime.length == 12) {
/*  309 */       b = this.dateTime[4];
/*      */     } else
/*  311 */       b = getBit(this.dateTime, offset + 0, 5);
/*  312 */     return (byte)b;
/*      */   }
/*      */   
/*      */   byte getMinute(int offset)
/*      */   {
/*      */     int b;
/*      */     int b;
/*  319 */     if (this.dateTime.length == 12) {
/*  320 */       b = this.dateTime[5];
/*      */     } else
/*  322 */       b = getBit(this.dateTime, offset + 5, 6);
/*  323 */     return (byte)b;
/*      */   }
/*      */   
/*      */   byte getSecond(int offset)
/*      */   {
/*      */     int b;
/*      */     int b;
/*  330 */     if (this.dateTime.length == 12) {
/*  331 */       b = this.dateTime[6];
/*      */     } else
/*  333 */       b = getBit(this.dateTime, offset + 11, 6);
/*  334 */     return (byte)b;
/*      */   }
/*      */   
/*      */   int getNano(int offset)
/*      */   {
/*      */     int b;
/*      */     int b;
/*  341 */     if (this.dateTime.length == 12)
/*      */     {
/*  343 */       byte[] msec = new byte[4];
/*  344 */       System.arraycopy(this.dateTime, 7, msec, 0, 3);
/*  345 */       b = Convertion.fourByteToInt(msec);
/*      */     }
/*      */     else {
/*  348 */       b = getBit(this.dateTime, offset + 17, 20); }
/*  349 */     return b;
/*      */   }
/*      */   
/*      */   public final Date getDate()
/*      */   {
/*  354 */     return new Date(getYear() - 1900, getMonth() - 1, getDay());
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public final Time getTime()
/*      */   {
/*  364 */     return new Time(getHour(0), getMinute(0), getSecond(0));
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public final String getTimeInString()
/*      */   {
/*  374 */     int hour = getHour(0);
/*  375 */     int min = getMinute(0);
/*  376 */     int sec = getSecond(0);
/*  377 */     int nano = getNano(0);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  382 */     String h = hour;
/*  383 */     String m = min;
/*  384 */     String s = sec;
/*  385 */     String n = nano;
/*      */     
/*  387 */     if (h.length() < 2)
/*      */     {
/*  389 */       h = "0" + h;
/*      */     }
/*      */     
/*  392 */     if (m.length() < 2)
/*      */     {
/*  394 */       m = "0" + m;
/*      */     }
/*      */     
/*  397 */     if (s.length() < 2)
/*      */     {
/*  399 */       s = "0" + s;
/*      */     }
/*      */     
/*  402 */     int nanoLen = n.length();
/*  403 */     if (nanoLen < this.prec)
/*      */     {
/*  405 */       int addLen = this.prec - nanoLen;
/*  406 */       for (int i = 0; i < addLen; i++) {
/*  407 */         n = "0" + n;
/*      */       }
/*      */     }
/*  410 */     if ((this.prec > 0) && (this.prec < 6) && (n.length() > this.prec))
/*      */     {
/*  412 */       n = n.substring(0, this.prec);
/*      */     }
/*      */     
/*  415 */     if (this.prec == 0) {
/*  416 */       return h + ":" + m + ":" + s;
/*      */     }
/*  418 */     return h + ":" + m + ":" + s + "." + n;
/*      */   }
/*      */   
/*      */   public final String getDateInString()
/*      */   {
/*  423 */     int year = getYear();
/*  424 */     int month = getMonth();
/*  425 */     int day = getDay();
/*      */     
/*  427 */     int sign = 0;
/*      */     
/*  429 */     if (year < 0)
/*      */     {
/*  431 */       sign = 1;
/*  432 */       year = Math.abs(year);
/*      */     }
/*      */     
/*  435 */     String y = year;
/*  436 */     String m = month;
/*  437 */     String d = day;
/*      */     
/*  439 */     if (y.length() < 4)
/*      */     {
/*  441 */       y = "0000".substring(0, 4 - y.length()) + y;
/*      */     }
/*      */     
/*  444 */     if (m.length() < 2)
/*      */     {
/*  446 */       m = "0" + m;
/*      */     }
/*      */     
/*  449 */     if (d.length() < 2)
/*      */     {
/*  451 */       d = "0" + d;
/*      */     }
/*      */     
/*  454 */     if (sign == 1)
/*      */     {
/*  456 */       y = "-" + y;
/*      */     }
/*      */     
/*  459 */     String strDate = y + "-" + m + "-" + d;
/*  460 */     return strDate;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public final String getTimeOfTimestamp()
/*      */   {
/*  470 */     int hour = getHour(24);
/*  471 */     int min = getMinute(24);
/*  472 */     int sec = getSecond(24);
/*      */     
/*  474 */     String h = hour;
/*  475 */     String m = min;
/*  476 */     String s = sec;
/*      */     
/*  478 */     if (h.length() < 2)
/*      */     {
/*  480 */       h = "0" + h;
/*      */     }
/*      */     
/*  483 */     if (m.length() < 2)
/*      */     {
/*  485 */       m = "0" + m;
/*      */     }
/*      */     
/*  488 */     if (s.length() < 2)
/*      */     {
/*  490 */       s = "0" + s;
/*      */     }
/*      */     
/*  493 */     return h + ":" + m + ":" + s;
/*      */   }
/*      */   
/*      */   public final Timestamp getTimestamp()
/*      */   {
/*  498 */     String str = String.valueOf(getDate()) + " " + getTimeOfTimestamp();
/*  499 */     Timestamp ts = Timestamp.valueOf(str);
/*  500 */     int x = 6 - this.prec;
/*  501 */     int nanos = getNano(24);
/*  502 */     String n = nanos;
/*  503 */     if ((x > 0) && (n.length() > x))
/*      */     {
/*  505 */       nanos = getNano(24) / (int)Math.pow(10.0D, x) * (int)Math.pow(10.0D, x);
/*      */     }
/*  507 */     else if (x > 0)
/*      */     {
/*  509 */       nanos = 0;
/*      */     }
/*  511 */     ts.setNanos(nanos * 1000);
/*  512 */     return ts;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static Date getDateByString(String s)
/*      */     throws SQLException
/*      */   {
/*  530 */     if (s == null)
/*      */     {
/*  532 */       throw new IllegalArgumentException();
/*      */     }
/*      */     
/*  535 */     int dividingSpace = s.indexOf(' ');
/*  536 */     if (dividingSpace > 0) {
/*  537 */       s = s.substring(0, dividingSpace);
/*      */     }
/*      */     
/*  540 */     int firstDash = s.indexOf('-');
/*  541 */     int secondDash = s.indexOf('-', firstDash + 1);
/*      */     
/*  543 */     if ((firstDash < 0) || (secondDash < 0))
/*      */     {
/*  545 */       firstDash = s.indexOf('.');
/*  546 */       secondDash = s.indexOf('.', firstDash + 1);
/*      */     }
/*      */     
/*  549 */     if ((firstDash < 0) || (secondDash < 0))
/*      */     {
/*  551 */       firstDash = s.indexOf('/');
/*  552 */       secondDash = s.indexOf('/', firstDash + 1);
/*      */     }
/*      */     int day;
/*  555 */     if (((firstDash > 0 ? 1 : 0) & (secondDash > 0 ? 1 : 0) & (secondDash < s.length() - 1 ? 1 : 0)) != 0)
/*      */     {
/*  557 */       int year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
/*  558 */       int month = Integer.parseInt(s.substring(firstDash + 1, secondDash)) - 1;
/*  559 */       day = Integer.parseInt(s.substring(secondDash + 1));
/*      */     }
/*      */     else
/*      */     {
/*  563 */       throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat")); }
/*      */     int day;
/*      */     int month;
/*  566 */     int year; if (!checkDate(year + 1900, month + 1, day)) {
/*  567 */       throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */     }
/*  569 */     return new Date(year, month, day);
/*      */   }
/*      */   
/*      */ 
/*      */   public static boolean dmdt_is_leap_year(int year)
/*      */   {
/*  575 */     if (year < 0) {
/*  576 */       year = -year;
/*      */     }
/*      */     
/*  579 */     if (year % 4 == 0)
/*      */     {
/*      */ 
/*  582 */       if ((year % 100 == 0) && (year % 400 != 0)) {
/*  583 */         return false;
/*      */       }
/*      */       
/*  586 */       return true;
/*      */     }
/*      */     
/*      */ 
/*  590 */     return false;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static boolean checkDate(int year, int month, int day)
/*      */   {
/*  600 */     if ((year > 9999) || (month > 12) || (month < 1)) {
/*  601 */       return false;
/*      */     }
/*  603 */     switch (month)
/*      */     {
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */     case 1: 
/*      */     case 3: 
/*      */     case 5: 
/*      */     case 7: 
/*      */     case 8: 
/*      */     case 10: 
/*      */     case 12: 
/*  617 */       if ((day > 31) || (day < 1)) {
/*  618 */         return false;
/*      */       }
/*      */       
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */       break;
/*      */     case 4: 
/*      */     case 6: 
/*      */     case 9: 
/*      */     case 11: 
/*  631 */       if ((day > 30) || (day < 1)) {
/*  632 */         return false;
/*      */       }
/*      */       
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */       break;
/*      */     case 2: 
/*  642 */       if (dmdt_is_leap_year(year))
/*      */       {
/*  644 */         if ((day > 29) || (day < 1)) {
/*  645 */           return false;
/*      */         }
/*  647 */       } else if ((day > 28) || (day < 1))
/*      */       {
/*  649 */         return false;
/*      */       }
/*      */       
/*      */ 
/*      */       break;
/*      */     }
/*      */     
/*      */     
/*  657 */     return true;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static boolean checkTime(int hour, int min, int sec, int msec)
/*      */   {
/*  667 */     if ((hour > 23) || (min > 59) || (sec > 59) || (msec > 999999)) {
/*  668 */       return false;
/*      */     }
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  675 */     return true;
/*      */   }
/*      */   
/*      */   public static boolean checkTimezone(short tz)
/*      */   {
/*  680 */     if (((tz < 64757) || (tz > 780)) && (tz != 1000)) {
/*  681 */       return false;
/*      */     }
/*  683 */     return true;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static Timestamp getTimestampByString(String s, int dtype, int scale, int ltz)
/*      */     throws SQLException
/*      */   {
/*  702 */     int[] dt = stringToDT(s, dtype, scale, ltz);
/*      */     
/*  704 */     return new Timestamp(dt[0] - 1900, dt[1] - 1, dt[2], dt[3], dt[4], dt[5], dt[6]);
/*      */   }
/*      */   
/*      */   public static int[] stringToDT(String s, int dtype, int scale, int ltz) throws SQLException
/*      */   {
/*  709 */     String date_s = null;
/*  710 */     String time_s = null;
/*  711 */     String nanos_s = null;
/*  712 */     String tz_s = null;
/*  713 */     int year = 0;
/*  714 */     int month = 0;
/*  715 */     int day = 0;
/*  716 */     int hour = 0;
/*  717 */     int minute = 0;
/*  718 */     int second = 0;
/*  719 */     int a_nanos = 0;
/*      */     
/*      */ 
/*      */ 
/*  723 */     int firstColon = 0;
/*  724 */     int secondColon = 0;
/*  725 */     int period = 0;
/*  726 */     int sign = 0;
/*      */     
/*  728 */     String zeros = "000000000";
/*      */     
/*  730 */     if (s == null)
/*      */     {
/*  732 */       throw new IllegalArgumentException("null string");
/*      */     }
/*      */     
/*      */ 
/*  736 */     s = s.trim();
/*  737 */     if (s.startsWith("-", 0))
/*      */     {
/*  739 */       s = s.substring(1).trim();
/*  740 */       sign = 1;
/*      */     }
/*      */     
/*  743 */     String[] comps = s.split(" ");
/*      */     
/*  745 */     if (comps.length > 1)
/*      */     {
/*      */ 
/*      */ 
/*  749 */       switch (comps.length)
/*      */       {
/*      */       case 3: 
/*  752 */         date_s = comps[0];
/*  753 */         time_s = comps[1];
/*  754 */         tz_s = comps[2];
/*  755 */         break;
/*      */       
/*      */       case 2: 
/*  758 */         date_s = comps[0];
/*  759 */         time_s = comps[1];
/*  760 */         break;
/*      */       
/*      */       default: 
/*  763 */         throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */       }
/*      */       
/*      */       
/*      */ 
/*      */ 
/*  769 */       int firstDash = date_s.indexOf('-');
/*  770 */       int secondDash = date_s.indexOf('-', firstDash + 1);
/*      */       
/*  772 */       if ((firstDash < 0) || (secondDash < 0))
/*      */       {
/*  774 */         firstDash = s.indexOf('.');
/*  775 */         secondDash = s.indexOf('.', firstDash + 1);
/*      */       }
/*      */       
/*  778 */       if ((firstDash < 0) || (secondDash < 0))
/*      */       {
/*  780 */         firstDash = s.indexOf('/');
/*  781 */         secondDash = s.indexOf('/', firstDash + 1);
/*      */       }
/*      */       
/*      */ 
/*  785 */       if (time_s == null)
/*      */       {
/*  787 */         throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */       }
/*  789 */       firstColon = time_s.indexOf(':');
/*  790 */       secondColon = time_s.indexOf(':', firstColon + 1);
/*  791 */       period = time_s.indexOf('.', secondColon + 1);
/*      */       
/*      */ 
/*  794 */       if (((firstDash > 0 ? 1 : 0) & (secondDash > 0 ? 1 : 0) & (secondDash < date_s.length() - 1 ? 1 : 0)) != 0)
/*      */       {
/*  796 */         if (sign == 1) {
/*  797 */           year = 0 - Integer.parseInt(date_s.substring(0, firstDash)) - 1900;
/*      */         } else {
/*  799 */           year = Integer.parseInt(date_s.substring(0, firstDash)) - 1900;
/*      */         }
/*  801 */         month = Integer.parseInt(date_s.substring(firstDash + 1, secondDash)) - 1;
/*  802 */         day = Integer.parseInt(date_s.substring(secondDash + 1));
/*      */ 
/*      */       }
/*      */       else
/*      */       {
/*  807 */         throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */       }
/*      */       
/*      */ 
/*  811 */       if (((firstColon > 0 ? 1 : 0) & (secondColon > 0 ? 1 : 0) & (secondColon < time_s.length() - 1 ? 1 : 0)) != 0)
/*      */       {
/*  813 */         hour = Integer.parseInt(time_s.substring(0, firstColon));
/*  814 */         minute = Integer.parseInt(time_s.substring(firstColon + 1, secondColon));
/*  815 */         if (((period > 0 ? 1 : 0) & (period < time_s.length() - 1 ? 1 : 0)) != 0)
/*      */         {
/*  817 */           second = Integer.parseInt(time_s.substring(secondColon + 1, period));
/*  818 */           nanos_s = time_s.substring(period + 1);
/*  819 */           if (nanos_s.length() > 9)
/*      */           {
/*  821 */             throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */           }
/*  823 */           if (!Character.isDigit(nanos_s.charAt(0)))
/*      */           {
/*  825 */             throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */           }
/*  827 */           nanos_s = nanos_s + zeros.substring(0, 9 - nanos_s.length());
/*      */           
/*  829 */           a_nanos = Integer.valueOf(nanos_s.substring(0, 6)).intValue();
/*      */         } else {
/*  831 */           if (period > 0)
/*      */           {
/*  833 */             throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */           }
/*      */           
/*      */ 
/*  837 */           second = Integer.parseInt(time_s.substring(secondColon + 1));
/*      */         }
/*      */       }
/*      */       else
/*      */       {
/*  842 */         throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */       }
/*      */       
/*  845 */       if (!checkDate(year + 1900, month + 1, day))
/*  846 */         throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*  847 */       if ((hour > 24) || (hour < 0) || (minute > 60) || (minute < 0) || (second > 60) || (second < 0)) {
/*  848 */         throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */       }
/*      */     }
/*      */     else {
/*  852 */       int dividingSpace = s.indexOf('-');
/*  853 */       int dividingSpace1 = s.indexOf('.');
/*  854 */       int dividingSpace2 = s.indexOf('/');
/*  855 */       if ((dividingSpace > 0) || (dividingSpace1 > 0) || (dividingSpace2 > 0))
/*      */       {
/*      */         try
/*      */         {
/*      */ 
/*  860 */           int firstDash = s.indexOf('-');
/*  861 */           int secondDash = s.indexOf('-', firstDash + 1);
/*      */           
/*  863 */           if ((firstDash < 0) || (secondDash < 0))
/*      */           {
/*  865 */             firstDash = s.indexOf('.');
/*  866 */             secondDash = s.indexOf('.', firstDash + 1);
/*      */           }
/*      */           
/*  869 */           if ((firstDash < 0) || (secondDash < 0))
/*      */           {
/*  871 */             firstDash = s.indexOf('/');
/*  872 */             secondDash = s.indexOf('/', firstDash + 1);
/*      */           }
/*      */           
/*  875 */           if (((firstDash > 0 ? 1 : 0) & (secondDash > 0 ? 1 : 0) & (secondDash < s.length() - 1 ? 1 : 0)) != 0)
/*      */           {
/*  877 */             if (sign == 1) {
/*  878 */               year = 0 - Integer.parseInt(s.substring(0, firstDash)) - 1900;
/*      */             } else {
/*  880 */               year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
/*      */             }
/*  882 */             month = Integer.parseInt(s.substring(firstDash + 1, secondDash)) - 1;
/*  883 */             day = Integer.parseInt(s.substring(secondDash + 1));
/*      */           }
/*      */           else
/*      */           {
/*  887 */             throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */           }
/*      */           
/*  890 */           if (checkDate(year + 1900, month + 1, day)) break label1204;
/*  891 */           throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */         }
/*      */         catch (Exception ex)
/*      */         {
/*  895 */           throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */         }
/*      */         
/*      */       }
/*      */       else
/*      */       {
/*      */         try
/*      */         {
/*  903 */           Time t = Time.valueOf(s);
/*      */           
/*  905 */           hour = t.getHours();
/*  906 */           minute = t.getMinutes();
/*  907 */           second = t.getSeconds();
/*  908 */           day = 1;
/*      */         }
/*      */         catch (Exception ex)
/*      */         {
/*  912 */           throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */         }
/*      */       }
/*      */     }
/*      */     
/*      */     label1204:
/*  918 */     if ((tz_s != null) && (DmdbType.isLocalTimeZone(dtype, scale)))
/*      */     {
/*  920 */       boolean neg = false;
/*  921 */       if (tz_s.startsWith("-")) {
/*  922 */         neg = true;
/*      */       }
/*  924 */       if ((tz_s.startsWith("-")) || (tz_s.startsWith("+"))) {
/*  925 */         tz_s = tz_s.substring(1).trim();
/*      */       }
/*  927 */       String[] hm = tz_s.split(":");
/*  928 */       short tzh = 0;
/*  929 */       short tzm = 0;
/*  930 */       switch (hm.length)
/*      */       {
/*      */       case 2: 
/*  933 */         tzh = Short.parseShort(hm[0].trim());
/*  934 */         tzm = Short.parseShort(hm[1].trim());
/*  935 */         break;
/*      */       
/*      */       case 1: 
/*  938 */         tzh = Short.parseShort(hm[0].trim());
/*  939 */         break;
/*      */       
/*      */       default: 
/*  942 */         throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */       }
/*      */       
/*  945 */       short ownTz = (short)(tzh * 60 + tzm);
/*  946 */       if (ownTz < 0) {
/*  947 */         throw new SQLException(Const.res.getString("error.invalidDateTimeTypeFormat"));
/*      */       }
/*  949 */       if (neg) {
/*  950 */         ownTz = (short)(ownTz * -1);
/*      */       }
/*      */       
/*  953 */       byte[] org = dmdtEncodeTz(year, month, day, hour, minute, second, a_nanos, ownTz);
/*  954 */       byte[] lorg = dmdtAddByFmt(org, 5, ltz - ownTz);
/*      */       
/*  956 */       year = Convertion.getShort(lorg, 0);
/*  957 */       month = Convertion.getByte(lorg, 2);
/*  958 */       day = Convertion.getByte(lorg, 3);
/*  959 */       hour = Convertion.getByte(lorg, 4);
/*  960 */       minute = Convertion.getByte(lorg, 5);
/*  961 */       second = Convertion.getByte(lorg, 6);
/*  962 */       a_nanos = Convertion.getByte(lorg, 7) + Convertion.getByte(lorg, 8) << 8 + Convertion.getByte(
/*  963 */         lorg, 9) << 16;
/*      */     }
/*      */     
/*  966 */     int[] dt = new int[7];
/*  967 */     dt[0] = (year + 1900);
/*  968 */     dt[1] = (month + 1);
/*  969 */     dt[2] = day;
/*  970 */     dt[3] = hour;
/*  971 */     dt[4] = minute;
/*  972 */     dt[5] = second;
/*  973 */     dt[6] = a_nanos;
/*      */     
/*  975 */     return dt;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static String getTimeFromTimestamp(Timestamp ts)
/*      */   {
/*  991 */     int hour = ts.getHours();
/*  992 */     int min = ts.getMinutes();
/*  993 */     int sec = ts.getSeconds();
/*  994 */     int msec = ts.getNanos();
/*      */     
/*  996 */     String h = hour;
/*  997 */     String m = min;
/*  998 */     String s = sec;
/*      */     
/* 1000 */     if (h.length() < 2)
/*      */     {
/* 1002 */       h = "0" + h;
/*      */     }
/*      */     
/* 1005 */     if (m.length() < 2)
/*      */     {
/* 1007 */       m = "0" + m;
/*      */     }
/*      */     
/* 1010 */     if (s.length() < 2)
/*      */     {
/* 1012 */       s = "0" + s;
/*      */     }
/*      */     
/* 1015 */     return h + ":" + m + ":" + s + "." + msec;
/*      */   }
/*      */   
/*      */ 
/*      */   public String getStringFromTimestamp(Timestamp ts)
/*      */   {
/* 1021 */     int year = ts.getYear();
/* 1022 */     int month = ts.getMonth();
/* 1023 */     int day = ts.getDate();
/* 1024 */     int hour = ts.getHours();
/* 1025 */     int minute = ts.getMinutes();
/* 1026 */     int second = ts.getSeconds();
/* 1027 */     int nanos = ts.getNanos();
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/* 1035 */     String zeros = "000000000";
/* 1036 */     String yearZeros = "0000";
/*      */     
/*      */     String yearString;
/* 1039 */     if (year < 1000)
/*      */     {
/*      */ 
/* 1042 */       String yearString = year;
/* 1043 */       yearString = yearZeros.substring(0, 4 - yearString.length()) + yearString;
/*      */     }
/*      */     else
/*      */     {
/* 1047 */       yearString = year; }
/*      */     String monthString;
/* 1049 */     String monthString; if (month < 10)
/*      */     {
/* 1051 */       monthString = "0" + month;
/*      */     }
/*      */     else
/*      */     {
/* 1055 */       monthString = Integer.toString(month); }
/*      */     String dayString;
/* 1057 */     String dayString; if (day < 10)
/*      */     {
/* 1059 */       dayString = "0" + day;
/*      */     }
/*      */     else
/*      */     {
/* 1063 */       dayString = Integer.toString(day); }
/*      */     String hourString;
/* 1065 */     String hourString; if (hour < 10)
/*      */     {
/* 1067 */       hourString = "0" + hour;
/*      */     }
/*      */     else
/*      */     {
/* 1071 */       hourString = Integer.toString(hour); }
/*      */     String minuteString;
/* 1073 */     String minuteString; if (minute < 10)
/*      */     {
/* 1075 */       minuteString = "0" + minute;
/*      */     }
/*      */     else
/*      */     {
/* 1079 */       minuteString = Integer.toString(minute); }
/*      */     String secondString;
/* 1081 */     String secondString; if (second < 10)
/*      */     {
/* 1083 */       secondString = "0" + second;
/*      */     }
/*      */     else
/*      */     {
/* 1087 */       secondString = Integer.toString(second); }
/*      */     String nanosString;
/* 1089 */     String nanosString; if (nanos == 0)
/*      */     {
/* 1091 */       nanosString = "0";
/*      */     }
/*      */     else
/*      */     {
/* 1095 */       nanosString = Integer.toString(nanos);
/*      */       
/*      */ 
/* 1098 */       nanosString = zeros.substring(0, 9 - nanosString.length()) + nanosString;
/*      */       
/*      */ 
/* 1101 */       char[] nanosChar = new char[nanosString.length()];
/* 1102 */       nanosString.getChars(0, nanosString.length(), nanosChar, 0);
/* 1103 */       int truncIndex = 8;
/* 1104 */       while (nanosChar[truncIndex] == '0')
/*      */       {
/* 1106 */         truncIndex--;
/*      */       }
/*      */       
/* 1109 */       nanosString = new String(nanosChar, 0, truncIndex + 1);
/*      */     }
/*      */     
/*      */ 
/* 1113 */     StringBuffer timestampBuf = new StringBuffer();
/* 1114 */     timestampBuf.append(yearString);
/* 1115 */     timestampBuf.append("-");
/* 1116 */     timestampBuf.append(monthString);
/* 1117 */     timestampBuf.append("-");
/* 1118 */     timestampBuf.append(dayString);
/* 1119 */     timestampBuf.append(" ");
/* 1120 */     timestampBuf.append(hourString);
/* 1121 */     timestampBuf.append(":");
/* 1122 */     timestampBuf.append(minuteString);
/* 1123 */     timestampBuf.append(":");
/* 1124 */     timestampBuf.append(secondString);
/* 1125 */     timestampBuf.append(".");
/* 1126 */     timestampBuf.append(nanosString);
/*      */     
/* 1128 */     return timestampBuf.toString();
/*      */   }
/*      */   
/*      */   int getBit(byte[] x, int start, int len)
/*      */   {
/* 1133 */     int xx0 = 0;
/* 1134 */     int start_bit = start & 0x7;
/* 1135 */     int s = start >> 3;
/* 1136 */     int e = (start + len - 1 >> 3) + 1;
/* 1137 */     int n = e - s;
/* 1138 */     int p = start >> 3;
/*      */     
/* 1140 */     if (n == 1)
/*      */     {
/* 1142 */       byte[] t1 = new byte[4];
/* 1143 */       System.arraycopy(x, p, t1, 0, 1);
/* 1144 */       xx0 = Convertion.fourByteToInt(t1);
/*      */     }
/* 1146 */     else if (n == 2)
/*      */     {
/* 1148 */       byte[] t1 = new byte[4];
/* 1149 */       System.arraycopy(x, p, t1, 0, 1);
/* 1150 */       int x1 = Convertion.fourByteToInt(t1);
/*      */       
/*      */ 
/* 1153 */       System.arraycopy(x, p + 1, t1, 0, 1);
/* 1154 */       int x2 = Convertion.fourByteToInt(t1);
/* 1155 */       x2 = x2 << 8 & 0xFFFFFFFF;
/* 1156 */       xx0 = x1 + x2;
/*      */     }
/* 1158 */     else if (n == 3)
/*      */     {
/* 1160 */       byte[] t1 = new byte[4];
/* 1161 */       System.arraycopy(x, p, t1, 0, 1);
/* 1162 */       int x1 = Convertion.fourByteToInt(t1);
/*      */       
/*      */ 
/* 1165 */       System.arraycopy(x, p + 1, t1, 0, 1);
/* 1166 */       int x2 = Convertion.fourByteToInt(t1);
/* 1167 */       x2 = x2 << 8 & 0xFFFFFFFF;
/*      */       
/*      */ 
/* 1170 */       System.arraycopy(x, p + 2, t1, 0, 1);
/* 1171 */       int x3 = Convertion.fourByteToInt(t1);
/* 1172 */       x3 = x3 << 16 & 0xFFFFFFFF;
/* 1173 */       xx0 = x1 + x2 + x3;
/*      */     }
/*      */     
/* 1176 */     return xx0 >> start_bit & -1 >>> 32 - len;
/*      */   }
/*      */   
/*      */   int getBit_ex(byte[] x)
/*      */   {
/* 1181 */     int len = 15;
/*      */     
/* 1183 */     byte[] t1 = new byte[4];
/*      */     
/*      */ 
/*      */ 
/*      */ 
/* 1188 */     System.arraycopy(x, 0, t1, 0, 1);
/* 1189 */     int x1 = Convertion.fourByteToInt(t1);
/*      */     
/* 1191 */     System.arraycopy(x, 1, t1, 0, 1);
/* 1192 */     int x2 = Convertion.fourByteToInt(t1);
/*      */     
/* 1194 */     x2 = x2 << 8 & 0xFFFFFFFF;
/* 1195 */     int xx0 = x1 + x2;
/*      */     
/* 1197 */     int year = xx0 >> 0 & -1 >>> 32 - len;
/*      */     
/* 1199 */     if (year > 9999) {
/* 1200 */       year = (short)((short)xx0 | 0x8000);
/*      */     }
/* 1202 */     return year;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static byte[] dmdtEncodeFast(byte[] dateTime)
/*      */   {
/* 1212 */     if (dateTime.length == 8) {
/* 1213 */       return dateTime;
/*      */     }
/* 1215 */     byte[] ret = new byte[8];
/*      */     
/* 1217 */     short year = Convertion.getShort(dateTime, 0);
/* 1218 */     byte month = dateTime[2];
/* 1219 */     byte day = dateTime[3];
/*      */     
/*      */ 
/* 1222 */     ret[0] = dateTime[0];
/*      */     
/*      */ 
/* 1225 */     ret[1] = ((byte)(year >> 8 | (month & 0x1) << 7));
/*      */     
/*      */ 
/* 1228 */     ret[2] = ((byte)((month & 0xE) >> 1 | day << 3));
/*      */     
/* 1230 */     byte hour = dateTime[4];
/* 1231 */     byte min = dateTime[5];
/* 1232 */     byte sec = dateTime[6];
/* 1233 */     int msec = (dateTime[7] & 0xFF) + ((dateTime[8] & 0xFF) << 8) + ((dateTime[9] & 0xFF) << 16);
/*      */     
/*      */ 
/* 1236 */     ret[3] = ((byte)(hour | (min & 0x7) << 5));
/*      */     
/*      */ 
/* 1239 */     ret[4] = ((byte)((min & 0x38) >> 3 | (sec & 0x1F) << 3));
/*      */     
/*      */ 
/* 1242 */     ret[5] = ((byte)((sec & 0x20) >> 5 | (msec & 0x7F) << 1));
/*      */     
/*      */ 
/* 1245 */     ret[6] = ((byte)(msec >> 7 & 0xFF));
/*      */     
/*      */ 
/* 1248 */     ret[7] = ((byte)(msec >> 15 & 0xFF));
/*      */     
/* 1250 */     return ret;
/*      */   }
/*      */   
/*      */ 
/*      */   byte[] dmdtDecodeFast()
/*      */   {
/* 1256 */     byte[] datetime = new byte[12];
/*      */     
/*      */ 
/* 1259 */     short year = getYear();
/* 1260 */     Convertion.setShort(datetime, 0, year);
/*      */     
/*      */ 
/* 1263 */     byte month = getMonth();
/* 1264 */     Convertion.setByte(datetime, 2, month);
/*      */     
/*      */ 
/* 1267 */     byte day = getDay();
/* 1268 */     Convertion.setByte(datetime, 3, day);
/*      */     
/*      */ 
/* 1271 */     byte hour = getHour(24);
/* 1272 */     Convertion.setByte(datetime, 4, hour);
/*      */     
/*      */ 
/* 1275 */     byte min = getMinute(24);
/* 1276 */     Convertion.setByte(datetime, 5, min);
/*      */     
/*      */ 
/* 1279 */     byte sec = getSecond(24);
/* 1280 */     Convertion.setByte(datetime, 6, sec);
/*      */     
/*      */ 
/* 1283 */     int msec = getNano(24);
/* 1284 */     Convertion.setByte(datetime, 7, (byte)(msec & 0xFF));
/* 1285 */     Convertion.setByte(datetime, 8, (byte)(msec >> 8 & 0xFF));
/* 1286 */     Convertion.setByte(datetime, 9, (byte)(msec >> 16 & 0xFF));
/*      */     
/* 1288 */     Convertion.setShort(datetime, 10, (short)0);
/*      */     
/* 1290 */     return datetime;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */   public static byte[] dateEncodeFast(byte[] dateTime)
/*      */   {
/* 1298 */     byte[] ret = new byte[3];
/*      */     
/* 1300 */     if (dateTime.length == 3) {
/* 1301 */       return dateTime;
/*      */     }
/* 1303 */     short year = Convertion.getShort(dateTime, 0);
/* 1304 */     byte month = dateTime[2];
/* 1305 */     byte day = dateTime[3];
/*      */     
/*      */ 
/* 1308 */     ret[0] = dateTime[0];
/*      */     
/*      */ 
/* 1311 */     ret[1] = ((byte)(year >> 8 | (month & 0x1) << 7));
/*      */     
/*      */ 
/* 1314 */     ret[2] = ((byte)((month & 0xE) >> 1 | day << 3));
/*      */     
/* 1316 */     return ret;
/*      */   }
/*      */   
/*      */ 
/*      */   static byte[] dmdtEncodeTz(int year, int month, int day, int hour, int min, int sec, int msec, short tz)
/*      */     throws SQLException
/*      */   {
/* 1323 */     if (!checkDate(year, month, day))
/*      */     {
/* 1325 */       if ((year == 0) && (month == 0) && (day == 0))
/*      */       {
/* 1327 */         Timestamp ts = new Timestamp(System.currentTimeMillis());
/*      */         
/* 1329 */         year = ts.getYear() + 1900;
/* 1330 */         month = ts.getMonth() + 1;
/* 1331 */         day = ts.getDay();
/*      */       }
/*      */       else
/*      */       {
/* 1335 */         DBError.throwSQLException(63024);
/* 1336 */         return null;
/*      */       }
/*      */     }
/*      */     
/* 1340 */     if (!checkTime(hour, min, sec, msec))
/*      */     {
/* 1342 */       DBError.throwSQLException(63024);
/* 1343 */       return null;
/*      */     }
/*      */     
/* 1346 */     if (!checkTimezone(tz))
/*      */     {
/* 1348 */       DBError.throwSQLException(63024);
/* 1349 */       return null;
/*      */     }
/*      */     
/* 1352 */     byte[] dt = new byte[12];
/*      */     
/* 1354 */     Convertion.setShort(dt, 0, (short)year);
/* 1355 */     Convertion.setByte(dt, 2, (byte)month);
/* 1356 */     Convertion.setByte(dt, 3, (byte)day);
/* 1357 */     Convertion.setByte(dt, 4, (byte)hour);
/* 1358 */     Convertion.setByte(dt, 5, (byte)min);
/* 1359 */     Convertion.setByte(dt, 6, (byte)sec);
/* 1360 */     Convertion.setByte(dt, 7, (byte)(msec & 0xFF));
/* 1361 */     Convertion.setByte(dt, 8, (byte)(msec >> 8 & 0xFF));
/* 1362 */     Convertion.setByte(dt, 9, (byte)(msec >> 16 & 0xFF));
/* 1363 */     Convertion.setShort(dt, 10, tz);
/*      */     
/* 1365 */     return dt;
/*      */   }
/*      */   
/*      */   public static byte[] dmdtAddByFmt(byte[] dt, int fmt, int n) throws SQLException
/*      */   {
/* 1370 */     if ((dt == null) || (dt.length != 12)) {
/* 1371 */       DBError.throwSQLException(63024);
/*      */     }
/* 1373 */     int year = Convertion.getShort(dt, 0);
/* 1374 */     int month = Convertion.getByte(dt, 2);
/* 1375 */     int day = Convertion.getByte(dt, 3);
/* 1376 */     int hour = Convertion.getByte(dt, 4);
/* 1377 */     int min = Convertion.getByte(dt, 5);
/* 1378 */     int sec = Convertion.getByte(dt, 6);
/* 1379 */     int msec = Convertion.getByte(dt, 7) & 0xFF | (Convertion.getByte(dt, 8) & 0xFF) << 8 | 
/* 1380 */       (Convertion.getByte(dt, 9) & 0xFF) << 16;
/*      */     
/* 1382 */     short tz = Convertion.getShort(dt, 10);
/*      */     
/*      */ 
/* 1385 */     int[] rets = null;
/* 1386 */     switch (fmt)
/*      */     {
/*      */     case 1: 
/* 1389 */       year += n;
/* 1390 */       break;
/*      */     
/*      */     case 8: 
/* 1393 */       n *= 3;
/*      */     
/*      */ 
/*      */     case 2: 
/* 1397 */       month += n;
/* 1398 */       year += month / 12;
/* 1399 */       month %= 12;
/* 1400 */       if (month < 1)
/*      */       {
/* 1402 */         month += 12;
/* 1403 */         year--;
/*      */       }
/*      */       
/*      */ 
/* 1407 */       if (day > Const.global_day_by_month[month])
/*      */       {
/* 1409 */         day = Const.global_day_by_month[month];
/*      */         
/* 1411 */         if ((month == 2) && (dmdt_is_leap_year(year)))
/* 1412 */           day++;
/*      */       }
/* 1414 */       break;
/*      */     
/*      */     case 7: 
/* 1417 */       msec = msec / 1000 + n;
/* 1418 */       n = msec / 1000;
/* 1419 */       msec %= 1000;
/* 1420 */       while (msec < 0)
/*      */       {
/* 1422 */         msec += 1000;
/* 1423 */         n--;
/*      */       }
/* 1425 */       msec *= 1000;
/* 1426 */       if (n == 0) {
/*      */         break;
/*      */       }
/*      */     case 6: 
/* 1430 */       int tmp = sec + n;
/* 1431 */       n = tmp / 60;
/* 1432 */       sec = tmp % 60;
/* 1433 */       if (sec < 0)
/*      */       {
/* 1435 */         sec += 60;
/* 1436 */         n--;
/*      */       }
/* 1438 */       if (n == 0) {
/*      */         break;
/*      */       }
/*      */     case 5: 
/* 1442 */       int tmp = min + n;
/* 1443 */       n = tmp / 60;
/* 1444 */       min = tmp % 60;
/* 1445 */       if (min < 0)
/*      */       {
/* 1447 */         min += 60;
/* 1448 */         n--;
/*      */       }
/* 1450 */       if (n == 0) {
/*      */         break;
/*      */       }
/*      */     case 4: 
/* 1454 */       int tmp = hour + n;
/* 1455 */       n = tmp / 24;
/* 1456 */       hour = tmp % 24;
/* 1457 */       if (hour < 0)
/*      */       {
/* 1459 */         hour += 24;
/* 1460 */         n--;
/*      */       }
/* 1462 */       if (n == 0) {
/*      */         break;
/*      */       }
/*      */     case 3: 
/*      */     case 9: 
/* 1467 */       rets = dmdtDateAdd(year, month, day, n);
/* 1468 */       year = rets[0];
/* 1469 */       month = rets[1];
/* 1470 */       day = rets[2];
/* 1471 */       break;
/*      */     
/*      */     case 10: 
/* 1474 */       rets = dmdtDateAdd(year, month, day, n * 7);
/* 1475 */       year = rets[0];
/* 1476 */       month = rets[1];
/* 1477 */       day = rets[2];
/* 1478 */       break;
/*      */     }
/*      */     
/*      */     
/*      */ 
/*      */ 
/* 1484 */     if (year > 9999) {
/* 1485 */       DBError.throwSQLException(63024);
/*      */     }
/* 1487 */     return dmdtEncodeTz(year, month, day, hour, min, sec, msec, tz);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   static int dmdt_calc_n_days_from_zero(int year, int month, int day)
/*      */   {
/* 1494 */     if ((year <= 0) && (month <= 0) && (day <= 0)) {
/* 1495 */       return 0;
/*      */     }
/* 1497 */     int ndays = year * 365 + Const.global_days_befor_month[(month - 1)] + day;
/*      */     
/*      */ 
/*      */ 
/* 1501 */     if (month <= 2) {
/* 1502 */       year--;
/*      */     }
/*      */     
/* 1505 */     int leap_days = year / 4 - year / 100 + year / 400;
/*      */     
/*      */ 
/* 1508 */     ndays += leap_days;
/*      */     
/* 1510 */     return ndays;
/*      */   }
/*      */   
/*      */ 
/*      */   static int[] dmdt_get_date_by_ndays_from_zero(int ndays)
/*      */   {
/* 1516 */     int[] retVals = new int[3];
/*      */     
/*      */ 
/* 1519 */     if ((ndays <= 365) || (ndays > 3652424))
/*      */     {
/* 1521 */       retVals[0] = (retVals[1] = retVals[2] = 99);
/* 1522 */       return retVals;
/*      */     }
/*      */     
/* 1525 */     int year = ndays * 100 / 36525;
/* 1526 */     int tmp = ((year - 1) / 100 + 1) * 3 / 4;
/* 1527 */     int days_left = ndays - year * 365 - (year - 1) / 4 + tmp;
/*      */     
/* 1529 */     int days_in_year = dmdt_is_leap_year(year) ? 366 : 365;
/*      */     
/* 1531 */     while (days_left > days_in_year)
/*      */     {
/* 1533 */       days_left -= days_in_year;
/* 1534 */       year++;
/* 1535 */       days_in_year = dmdt_is_leap_year(year) ? 366 : 365;
/*      */     }
/*      */     
/* 1538 */     if (days_in_year == 366)
/*      */     {
/* 1540 */       if (days_left > 59)
/*      */       {
/* 1542 */         days_left--;
/*      */         
/*      */ 
/* 1545 */         if (days_left == 59)
/*      */         {
/* 1547 */           retVals[0] = year;
/* 1548 */           retVals[1] = 2;
/* 1549 */           retVals[2] = 29;
/* 1550 */           return retVals;
/*      */         }
/*      */       }
/*      */     }
/*      */     
/*      */ 
/* 1556 */     for (int month = 1; 
/* 1557 */         days_left > Const.global_day_by_month[month]; month++) {
/* 1558 */       days_left -= Const.global_day_by_month[month];
/*      */     }
/* 1560 */     retVals[0] = year;
/* 1561 */     retVals[1] = month;
/* 1562 */     retVals[2] = days_left;
/*      */     
/* 1564 */     return retVals;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   static int[] dmdtDateAdd(int year, int month, int day, int n)
/*      */   {
/* 1571 */     int ndays = dmdt_calc_n_days_from_zero(year, month, day);
/*      */     
/*      */ 
/* 1574 */     ndays += n;
/*      */     
/*      */ 
/* 1577 */     return dmdt_get_date_by_ndays_from_zero(ndays);
/*      */   }
/*      */   
/*      */   public String getTimestampInString()
/*      */   {
/* 1582 */     String str = getDateInString() + " " + getTimeOfTimestamp();
/* 1583 */     String n = getNano(24);
/* 1584 */     int nanoLen = n.length();
/*      */     
/* 1586 */     if (this.prec == 0) {
/* 1587 */       return str;
/*      */     }
/* 1589 */     if (nanoLen < this.prec)
/*      */     {
/* 1591 */       int addLen = this.prec - nanoLen;
/* 1592 */       for (int i = 0; i < addLen; i++) {
/* 1593 */         n = "0" + n;
/*      */       }
/*      */     }
/* 1596 */     if ((this.prec > 0) && (this.prec < 6) && (nanoLen > this.prec))
/*      */     {
/* 1598 */       n = n.substring(0, this.prec);
/*      */     }
/*      */     
/* 1601 */     return str + "." + n;
/*      */   }
/*      */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\sql\DmdbDateTime.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */